package gbase8s

import (
	"os"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var (
	log *zap.Logger
)

var (
	Any    = zap.Any
	Stack  = zap.Stack
	String = zap.String
	Int    = zap.Int
)

func init() {
	// 创建自定义配置
	config := zap.NewProductionConfig()
	config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder

	config.Level = initLevel()

	// 构建日志记录器
	log, _ = config.Build(zap.AddStacktrace(zapcore.ErrorLevel))

	// 设置全局日志记录器
	zap.ReplaceGlobals(log)

}

func initLevel() zap.AtomicLevel {
	logLevel := os.Getenv("LOG_LEVEL")
	var level zap.AtomicLevel
	switch logLevel {
	case "debug":
		level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
	case "info":
		level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
	case "warn":
		level = zap.NewAtomicLevelAt(zapcore.WarnLevel)
	case "error":
		level = zap.NewAtomicLevelAt(zapcore.ErrorLevel)
	case "dpanic":
		level = zap.NewAtomicLevelAt(zapcore.DPanicLevel)
	case "panic":
		level = zap.NewAtomicLevelAt(zapcore.PanicLevel)
	case "fatal":
		level = zap.NewAtomicLevelAt(zapcore.FatalLevel)
	default:
		level = zap.NewAtomicLevelAt(zapcore.InfoLevel) // default InfoLevel
	}

	return level
}
